package com.system.blogAdmin.utils;

import com.cnc.common.lang.page.PageBean;
import com.cnc.common.lang.utils.BeanUtils;
import com.cnc.common.web.ezPage.PageUtils;
import com.system.blogAdmin.auth.dto.*;
import com.system.blogAdmin.auth.entity.AuthResource;
import com.system.blogAdmin.auth.entity.AuthRole;
import com.system.blogAdmin.auth.entity.User;
import com.system.blogAdmin.blog.dto.ArticleDto;
import com.system.blogAdmin.blog.dto.CategoryDto;
import com.system.blogAdmin.blog.entity.BlogArticle;
import com.system.blogAdmin.blog.entity.BlogCategory;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @描述: TODO(这里用一句话描述这个类的作用)
 * @作者: haw
 * @创建时间: 2017-01-09
 * @版本: 1.0
 */
public class DtoUtils {
    private static Logger logger = LoggerFactory.getLogger(DtoUtils.class);
    /**
     * 树结构的转化类
     * @param resourceList
     * @return
     */
    public static  List<TreeDto> res2treeDto(List<AuthResource> resourceList) {
        List<TreeDto> tree = null;
        if(resourceList != null && resourceList.size()>0){
            tree = new ArrayList<TreeDto>();
            for (AuthResource resource : resourceList) {
                TreeDto dto = new TreeDto(resource.getId(),resource.getName(),resource.getPid(),false,resource.getDisplay());
                tree.add(dto);
            }
        }
        return  tree;
    }
    /***
     * 将所有的资源和角色拥有的资源整合，角色拥有的将checked = true
     * @param allList
     * @param roleList
     * @return
     */
    public static List<TreeDto> res2treeDto(List<AuthResource> allList,List<AuthResource> roleList) {
        if(roleList == null || roleList.size()<=0){
            return res2treeDto(allList);
        }

        List<TreeDto> tree = null;
        if(allList != null && allList.size()>0){
            tree = new ArrayList<TreeDto>();
            for (AuthResource resource : allList) {
                if(roleList.toString().contains(resource.toString())){
                    TreeDto dto = new TreeDto(resource.getId(),resource.getName(),resource.getPid(),true,resource.getDisplay());
                    tree.add(dto);
                }else{
                    TreeDto dto = new TreeDto(resource.getId(),resource.getName(),resource.getPid(),false,resource.getDisplay());
                    tree.add(dto);
                }
            }
        }
        return  tree;
    }

    public static  List<AuthResDto> res2resDto(List<AuthResource> resourceList){
        List<AuthResDto> list = new ArrayList<AuthResDto>();
        BeanUtils.copyListProperties(resourceList, list, AuthResDto.class);
        return list;
    }
    public static  List<CategoryDto> categoryToDto(List<BlogCategory> resourceList){
        List<CategoryDto> list = new ArrayList<CategoryDto>();
        BeanUtils.copyListProperties(resourceList, list, CategoryDto.class);
        return list;
    }
    /**
     * 将所有的角色资源和用户拥有的角色整合，用户拥有的将checked=true;
     * @param userRoles 用户拥有的角色
     * @param allRoles  所有的角色
     * @return 所有的角色，用户拥有的角色checked=true，其他checked=false;
     */
    public static List<AuthRoleDto> roles2roleDto(List<AuthRole> allRoles, List<AuthRole> userRoles) {
        List<AuthRoleDto> list = new ArrayList<AuthRoleDto>();
        if(allRoles != null){
            if(userRoles == null){
                BeanUtils.copyListProperties(allRoles,list,AuthResDto.class);
                return list;
            }else{
                for (AuthRole role : allRoles) {
                    if(userRoles.toString().contains(role.toString())){
                        list.add(new AuthRoleDto(role,true));
                    }else {
                        list.add(new AuthRoleDto(role,false));
                    }
                }
            }
        }
        return list;
    }

    public static UserDto userToUserDto(User user){
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setIsLock(user.getIsLock());
        userDto.setIntroduce(user.getIntroduce());
        userDto.setAccount(user.getAccount());
        userDto.setJob(user.getJob());
        userDto.setMobilePhone(user.getMobilePhone());
        userDto.setUserImg(user.getUserImg());
        userDto.setName(user.getName());
        userDto.setCreateTime(user.getCreateTime());
        userDto.setModifyTime(user.getModifyTime());
        userDto.setName(user.getName());
        userDto.setEmail(user.getEmail());
        return userDto;
    }


    public static PageBean<ArticleDto> ArticleToBlog(PageBean<BlogArticle> blogArticlePageBean){
        List<BlogArticle> blogArticleList = blogArticlePageBean.getRecordList();
        PageBean<ArticleDto> articleDtoPageBean = new PageBean<ArticleDto>();
        List<ArticleDto> articleDtos = new ArrayList<ArticleDto>();
        for (BlogArticle blogArticle : blogArticleList) {
            ArticleDto articleDto = new ArticleDto();
            articleDto.setId(blogArticle.getId());
            articleDto.setReadQuantity(blogArticle.getReadQuantity());
            articleDto.setContent(blogArticle.getContent());
            articleDto.setLabel(blogArticle.getLabel());
            articleDto.setTitle(blogArticle.getTitle());
            articleDto.setCreateTime(blogArticle.getCreateTime());
            articleDto.setRecommend(blogArticle.getRecommend());
            articleDto.setCategory(blogArticle.getCategoryName());
            articleDto.setCreateName(blogArticle.getCreateName());
            articleDto.setCategoryId(Long.parseLong(blogArticle.getCategoryId().toString()));
            articleDtos.add(articleDto);

        }
        articleDtoPageBean.setRecordList(articleDtos);
        articleDtoPageBean.setTotalCount(blogArticlePageBean.getTotalCount());
        articleDtoPageBean.setPageCount(blogArticlePageBean.getPageCount());
        articleDtoPageBean.setNumPerPage(blogArticlePageBean.getNumPerPage());
        articleDtoPageBean.setBeginPageIndex(blogArticlePageBean.getBeginPageIndex());
        articleDtoPageBean.setEndPageIndex(blogArticlePageBean.getEndPageIndex());
        articleDtoPageBean.setCountResultMap(blogArticlePageBean.getCountResultMap());
        articleDtoPageBean.setCurrentPage(blogArticlePageBean.getCurrentPage());
//        logger.info("具体数据:" + articleDtos.get(0).toString());
        return articleDtoPageBean;
    }
    public static List<ArticleDto> ArticleToBlog(BlogArticle blogArticle){
        List<ArticleDto> articleDtos = new ArrayList<ArticleDto>();
        ArticleDto articleDto = new ArticleDto();
        articleDto.setId(blogArticle.getId());
        articleDto.setReadQuantity(blogArticle.getReadQuantity());
        articleDto.setContent(blogArticle.getContent());
        articleDto.setLabel(blogArticle.getLabel());
        articleDto.setTitle(blogArticle.getTitle());
        articleDto.setCreateTime(blogArticle.getCreateTime());
        articleDto.setRecommend(blogArticle.getRecommend());
        articleDto.setCategory(blogArticle.getCategoryName());
        articleDto.setCreateName(blogArticle.getCreateName());
        articleDto.setCategoryId(Long.parseLong(blogArticle.getCategoryId().toString()));
        articleDtos.add(articleDto);
        return articleDtos;
    }

    /**
     * 转换实体类
     * @param userList
     * @return
     */
    public static List<AuthUserDto> toChange(List<User> userList){
        List<AuthUserDto> authlist = new ArrayList<AuthUserDto>();
        for (User user : userList) {
            AuthUserDto au = new AuthUserDto();
            au.setAccount(user.getAccount());
            au.setId(user.getId());
            au.setStatus(user.getIsLock());
            authlist.add(au);
        }
        return authlist;
    }
}
